const Router = require('koa-router')
const { catchError, generateOk, check } = require('../../lib/check')
const { getAllReports, searchReport, editReport } = require('../../model/report')
const { searchAccountById, searchAccount } = require('../../model/accounts')
const { searchArticle, editArticle } = require('../../model/articles')

let report = new Router()

report.post('/get', async (ctx) => {
  try {
    const data = ctx.request.body
    const reports = getAllReports()
    let newReports = reports
    if (data.create_Ts === 'descend') {
      newReports = newReports.sort((a, b) => Number(b.create_Ts) - Number(a.create_Ts))
    } else {
      newReports = newReports.sort((a, b) => Number(a.create_Ts) - Number(b.create_Ts))
    }
    if (!!data.userId) {
      const findAccount = searchAccountById(data.userId)
      check(findAccount, 'No_Such_Account')
      newReports = newReports.filter(item => item.userId === data.userId)
    }
    if (data.type !== undefined) {
      newReports = newReports.filter(item => item.type.findIndex(item => item === data.type) !== -1)
    }
    if (data.targetType !== undefined) {
      newReports = newReports.filter(item => item.targetType === data.targetType)
    }
    if (data.status !== undefined) {
      newReports = newReports.filter((item) => item.status === data.status)
    }
    if (data.content !== undefined) {
      newReports = newReports.filter((item) => item.content.includes(data.content))
    }
    const start = data.offset === undefined ? 0 : data.offset
    const size = data.limit === undefined ? 7 : data.limit
    const resData = newReports.slice(start, start + size)
    ctx.body = generateOk({ data: resData, total: newReports.length, length: resData.length })
  } catch (e) {
    catchError(e, ctx)
  }
})

report.post('/changeStatu', async (ctx) => {
  try {
    const data = ctx.request.body
    check(!!data.id, 'Params_Lost')
    // check(!!data.id, 'Params_Lost')
    // check(data.state !== undefined, 'Params_Lost')
    const findReport = searchReport(data.id)
    check(findReport, 'Not_Report')
    editReport({ ...findReport, status: findReport.status === 0 ? 1 : 0 })
    ctx.body = generateOk({ ...data, status: !findReport.status })
  } catch (e) {
    catchError(e, ctx)
  }
})

report.post('/ban', async (ctx) => {
  try {
    let result
    const data = ctx.request.body
    check(!(data.articleId === undefined), "Article_UnPointOut")
    // check(!(data.userId === undefined), "User_UnPointOut")
    check(((data.targetType === 'article') || (data.targetType === 'comment') || (data.targetType === 'reply')), 'Wrong_Type')
    const findArticle = searchArticle(data.articleId)
    check(!!findArticle, "No_Such_Article")
    check(!!searchAccountById(data.userId), "No_Such_User")
    if (data.type === 'article') {
      result = { ...findArticle, status: !findArticle.status }
    } else {
      const findComment = findArticle.comments.find(comment => comment.id === data.commentId)
      check(findComment, 'No_Such_Comment')
      if (data.type === 'comment') {
        let resultComment
        resultComment = { ...findComment, status: !findComment.status }
        findArticle.comments.splice(findArticle.comments.findIndex(comment => comment.id === findComment.id), 1, resultComment)
      } else {
        const findReply = findComment.reply.find(reply => reply.id === data.replyId)
        check(findReply, 'No_Such_Reply')
        let resultReply
        resultReply = { ...findReply, status: !findReply.status }
        findComment.reply.splice(findComment.reply.findIndex(reply => reply.id === findReply.id), 1, resultReply)
        findArticle.comments.splice(findArticle.comments.findIndex(comment => comment.id === findComment.id), 1, { ...findComment })
      }
      result = { ...findArticle }
    }
    editArticle(result)
    // reFreshToken(ctx)
    ctx.body = generateOk(result)
  } catch (e) {
    catchError(e, ctx)
  }
})

report.post('/changeDone', async (ctx) => {
  try {
    const data = ctx.request.body
    check(!!data.id, 'Params_Lost')
    // check(!!data.id, 'Params_Lost')
    // check(data.state !== undefined, 'Params_Lost')
    const findReport = searchReport(data.id)
    check(findReport, 'Not_Report')
    editReport({ ...findReport, done: findReport.done === 0 ? 1 : 0 })
    ctx.body = generateOk({ ...data, done: !data.done })
  } catch (e) {
    catchError(e, ctx)
  }
})

report.post('/changeStatus', async (ctx) => {
  try {
    const data = ctx.request.body
    check(!!data.id, 'Params_Lost')
    // check(!!data.id, 'Params_Lost')
    // check(data.state !== undefined, 'Params_Lost')
    const findReport = searchReport(data.id)
    check(findReport, 'Not_Report')
    editReport({ ...findReport, status: findReport.status === 0 ? 1 : 0 })
    ctx.body = generateOk({ ...data, status: !data.status })
  } catch (e) {
    catchError(e, ctx)
  }
})

module.exports = report